using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Voodoo.Meta
{
	public class LeaderboardPlayerSlot : MonoBehaviour
	{
		[SerializeField]
		private TMP_Text _rankTxt;

		[SerializeField]
		private TMP_Text _playerName;

		[SerializeField]
		private TMP_Text _score;

		[SerializeField]
		private Image _background;

		[SerializeField]
		private Image _rankBackground;

		[SerializeField]
		private GameObject _outline;

		[SerializeField]
		private Image _medal;

		[SerializeField]
		private List<GameObject> _fxValidation;

		[Header("Optional")]
		[Tooltip("Remove if you don't want the player's name to be editable, remember to deactivate the GameObject too.")]
		[SerializeField]
		private TMP_InputField _playerNameInputField;

		[Tooltip("Remove if you don't want the player's flag to be visible, remember to deactivate the GameObject too.")]
		[SerializeField]
		private Image _flagIcon;

		private int _rank;

		private RectTransform _rectTransform;

		private LeaderboardSlotInfo _slotInfo;

		public float Height => RectTransform.sizeDelta.y;

		public Vector2 AnchoredPosition
		{
			get
			{
				return RectTransform.anchoredPosition;
			}
			set
			{
				RectTransform.anchoredPosition = value;
			}
		}

		public int Rank
		{
			get
			{
				return _rank;
			}
			set
			{
				_rank = value;
				_rankTxt.text = "#" + _rank;
			}
		}

		public RectTransform RectTransform
		{
			get
			{
				if (_rectTransform == null)
				{
					_rectTransform = base.transform as RectTransform;
				}
				return _rectTransform;
			}
		}

		public event Action<string> nameChanged;

		public void Initialize(LeaderboardSlotInfo slotInfo)
		{
			Refresh(slotInfo);
			_ = _flagIcon != null;
			for (int i = 0; i < _fxValidation.Count; i++)
			{
				_fxValidation[i].SetActive(value: false);
			}
		}

		public void Refresh(LeaderboardSlotInfo slotInfo)
		{
			_slotInfo = slotInfo;
			AnchoredPosition = _slotInfo.anchoredPosition;
			Rank = _slotInfo.rank;
			SetFlag(_slotInfo.playerInfo.playerFlag);
			SetSlotName(_slotInfo.playerInfo.playerName, _slotInfo.showScore);
			_background.color = _slotInfo.backgroundColors.backgroundColor;
			_rankBackground.color = _slotInfo.backgroundColors.rankBackgroundColor;
			_score.text = _slotInfo.score.ToString();
			_score.gameObject.SetActive(_slotInfo.showScore);
			_outline.SetActive(_slotInfo.showScore);
			SetMedal(_slotInfo.medal);
		}

		public void Refresh(int rank, LeaderboardBackgroundColors backgroundColors, Sprite medal)
		{
			Rank = rank;
			_background.color = backgroundColors.backgroundColor;
			_rankBackground.color = backgroundColors.rankBackgroundColor;
			SetMedal(medal);
		}

		public void MoveSlot(float margin, int direction)
		{
			AnchoredPosition += new Vector2(0f, (Height + margin) * (float)direction);
		}

		private void SetSlotName(string slotName, bool isPlayer)
		{
			_playerName.text = slotName;
			if (_playerNameInputField != null)
			{
				_playerName.gameObject.SetActive(!isPlayer);
				_playerNameInputField.gameObject.SetActive(isPlayer);
				if (isPlayer)
				{
					_playerNameInputField.text = slotName;
				}
			}
			else
			{
				_playerName.gameObject.SetActive(value: true);
			}
		}

		public void UpdateScore(int score)
		{
			_score.text = score.ToString();
		}

		public void OnValidation()
		{
			for (int i = 0; i < _fxValidation.Count; i++)
			{
				_fxValidation[i].SetActive(value: true);
			}
		}

		public void OnPlayerNameChanged(string newName)
		{
			_slotInfo.playerInfo.playerName = newName;
			_playerNameInputField.text = newName;
			this.nameChanged?.Invoke(newName);
		}

		private void SetFlag(Sprite flag)
		{
			if (_flagIcon != null)
			{
				if (flag != null)
				{
					_flagIcon.sprite = flag;
					_flagIcon.enabled = true;
				}
				else
				{
					_flagIcon.sprite = null;
					_flagIcon.enabled = false;
				}
			}
		}

		private void SetMedal(Sprite medal)
		{
			_medal.gameObject.SetActive(medal != null);
			_rankTxt.gameObject.SetActive(_medal != null);
			if (medal != null)
			{
				_medal.sprite = medal;
			}
		}
	}
}
